﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Web;
using System.Web.SessionState;
using DTcms.Web.UI;
using DTcms.Common;

namespace DTcms.Web.m
{
    /// <summary>
    /// index 的摘要说明
    /// </summary>
    public class main : IHttpHandler, IRequiresSessionState
    {
        protected int totalCount = 0;
        public void ProcessRequest(HttpContext context)
        {
            int type_id = DTRequest.GetQueryInt("type_id", 0);
            ///添加产品分类筛选条件  Demon 2018-11-30
            int category_id = DTRequest.GetQueryInt("category_id", 0);
            StringBuilder strTxt = new StringBuilder();
            strTxt.Append("{");

            //获取公告
            //修改为获取支付记录  修改于2018-12-12 Demon
            BLL.news bll_n = new BLL.news();
            DataSet ds_n = bll_n.GetList(1, "status=1 and category_id=7", "id desc");
            //如果记录存在
            if (ds_n.Tables[0].Rows.Count > 0)
            {
                strTxt.Append("\"notice_list\":[");
                for (int i = 0; i < ds_n.Tables[0].Rows.Count; i++)
                {
                    DataRow dr_n = ds_n.Tables[0].Rows[i];
                    strTxt.Append("{");
                    strTxt.Append("\"id\":" + dr_n["id"]);
                    strTxt.Append(",\"remark\":\"" + Microsoft.JScript.GlobalObject.escape(dr_n["title"].ToString()) + "\"");
                    strTxt.Append("}");
                    //是否加逗号
                    if (i < ds_n.Tables[0].Rows.Count - 1)
                    {
                        strTxt.Append(",");
                    }
                }
                strTxt.Append("],");
            }
            else
            {
                strTxt.Append("\"notice_list\":[],");
            }
            BLL.member_order bll = new BLL.member_order();
            BLL.users userbll = new BLL.users();
            BLL.company combll = new BLL.company();
            DataSet ds = bll.GetList(5, " payment_status=1 and user_id in(select user_id from ch_company ) ", "payment_time desc");
            if (ds.Tables[0].Rows.Count > 0)
            {
                strTxt.Append("\"member_order\":[");
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    DataRow dr = ds.Tables[0].Rows[i];
                    strTxt.Append("{");
                    strTxt.Append("\"group_id\":" + dr["group_id"]);
                    strTxt.Append(",\"user_id\":" + dr["user_id"]);
                    strTxt.Append(",\"user_name\":\"" + userbll.GetUserName(int.Parse(dr["user_id"].ToString())) + "\"");
                    //strTxt.Append(",\"company_id\":\"" + dr["com_title"] + "\"");
                    //strTxt.Append(",\"company_name\":\"" + dr["com_id"] + "\"");
                    //strTxt.Append(",\"company_mobile\":\"" + dr["com_mobile"] + "\"");
                    Model.company com = combll.GetModel(combll.GetCompanyID(int.Parse(dr["user_id"].ToString())));
                    if (com != null)
                    {
                        strTxt.Append(",\"company_id\":\"" + com.id + "\"");
                        string com_title = com.title;
                        if (com_title.Length > 10) { com_title = com_title.Substring(0, 10)+".."; }
                        strTxt.Append(",\"company_name\":\"" + com_title + "\"");
                        strTxt.Append(",\"company_mobile\":\"" + com.mobile + "\"");
                    }
                    else
                    {
                        strTxt.Append(",\"company_id\":\"\"");
                        strTxt.Append(",\"company_name\":\"\"");
                        strTxt.Append(",\"company_mobile\":\"\"");
                    }

                    strTxt.Append("}");
                    //是否加逗号
                    if (i < ds.Tables[0].Rows.Count - 1)
                    {
                        strTxt.Append(",");
                    }

                }
                strTxt.Append("],");
            }
            else { strTxt.Append("\"member_order\":[],"); }
            //获取询价
            BLL.xunjia bll_x = new BLL.xunjia();
            ///###### 2018-11-30 Demon Update 询价信息修改为显示4条
            DataSet ds_x = bll_x.GetList(4, "status>0", "id desc");
            //如果记录存在
            if (ds_x.Tables[0].Rows.Count > 0)
            {
                strTxt.Append("\"xunjia_list\":[");
                for (int i = 0; i < ds_x.Tables[0].Rows.Count; i++)
                {
                    DataRow dr_x = ds_x.Tables[0].Rows[i];
                    strTxt.Append("{");
                    strTxt.Append("\"id\":" + dr_x["id"]);
                    string _remark = Microsoft.JScript.GlobalObject.escape(dr_x["remark"].ToString());
                    strTxt.Append(",\"remark\":\"" + _remark + "\"");
                    strTxt.Append(",\"remark_con\":\"" + Microsoft.JScript.GlobalObject.encodeURI(dr_x["remark"].ToString()) + "\"");
                    strTxt.Append(",\"add_time\":\"" + Utils.DateStringFromNow(Convert.ToDateTime(dr_x["add_time"])) + "\"");
                    int _user_id = int.Parse(dr_x["user_id"].ToString());
                    string _user_name = new BLL.users().GetUserName(_user_id);
                    string _avatar = new BLL.users().GetAvatar(_user_id);
                    strTxt.Append(",\"user_name\":\"" + _user_name + "\"");
                    string _group_id = new BLL.users().GetGroupID(_user_id).ToString();
                    strTxt.Append(",\"group_id\":\"" + _group_id + "\"");
                    if (string.IsNullOrEmpty(_avatar))
                    {
                        strTxt.Append(",\"avatar\":\"../image/default_avatar.png\"");
                    }
                    else if (_avatar.Contains("shicaiquanzi"))
                    {
                        //strTxt.Append(",\"avatar\":\"http://" + _avatar + "\"");
                        if (_avatar.Contains("http://"))
                        {
                            strTxt.Append(",\"avatar\":\"" + _avatar + "\"");

                        }
                        else { strTxt.Append(",\"avatar\":\"http://" + _avatar + "\""); }
                    }
                    else
                    {
                        strTxt.Append(",\"avatar\":\"http://pic.shicaiquanzi.com" + _avatar + "\"");
                    }
                    strTxt.Append(",\"hits\":" + dr_x["hits"]);
                    strTxt.Append("}");
                    //是否加逗号
                    if (i < ds_x.Tables[0].Rows.Count - 1)
                    {
                        strTxt.Append(",");
                    }
                }
                strTxt.Append("],");
            }
            //获取供应信息
            BLL.index_company bll2 = new BLL.index_company();
            DataSet ds2 = bll2.GetList(9, "", "sort_id asc,id desc");
            //如果记录存在
            if (ds2.Tables[0].Rows.Count > 0)
            {
                strTxt.Append("\"index_company\":[");
                for (int i = 0; i < ds2.Tables[0].Rows.Count; i++)
                {
                    DataRow dr2 = ds2.Tables[0].Rows[i];
                    strTxt.Append("{");
                    strTxt.Append("\"company_id\":" + dr2["company_id"]);
                    strTxt.Append(",\"title\":\"" + dr2["title"] + "\"");
                    string picurl = dr2["img_url"].ToString();

                    if (picurl.Contains("shicaiquanzi"))
                    {
                        if (picurl.Contains("http://"))
                        {
                            strTxt.Append(",\"img_url\":\"" + picurl + "\"");
                          
                        }
                        else { strTxt.Append(",\"img_url\":\"http://" + picurl + "\"");  }
                    }
                    else
                    {
                        strTxt.Append(",\"img_url\":\"http://pic.shicaiquanzi.com" + picurl + "\"");
                       
                    }

                 
                    string _mobile = new BLL.company().GetMobile(Convert.ToInt32(dr2["company_id"]));
                    strTxt.Append(",\"mobile\":\"" + _mobile + "\"");
                    strTxt.Append("}");
                    //是否加逗号
                    if (i < ds2.Tables[0].Rows.Count - 1)
                    {
                        strTxt.Append(",");
                    }
                }
                strTxt.Append("],");
            }
            ///########2018-11-30  Demon update 全国圈去掉产品模块
            //获取产品信息
            BLL.product bll4 = new BLL.product();
            DataSet ds4 = bll4.GetList(9, "status=1", "add_time desc");
            //如果记录存在
            if (ds4.Tables[0].Rows.Count > 0)
            {
                strTxt.Append("\"product_list\":[");
                for (int i = 0; i < ds4.Tables[0].Rows.Count; i++)
                {
                    DataRow dr4 = ds4.Tables[0].Rows[i];
                    strTxt.Append("{");
                    strTxt.Append("\"id\":" + dr4["id"]);
                    if (dr4["title"].ToString().Length > 7)
                    {
                        strTxt.Append(",\"title\":\"" + dr4["title"].ToString().Substring(0, 7) + "\"");
                    }
                    else
                    {
                        strTxt.Append(",\"title\":\"" + dr4["title"] + "\"");
                    }

                    if (Convert.ToInt32(dr4["price"]) > 0)
                    {
                        strTxt.Append(",\"market_price\":\"" + dr4["price"] + "元/" + dr4["unit"] + "\"");
                    }
                    else
                    {
                        strTxt.Append(",\"market_price\":\"面议\"");
                    }
                    string picurl = dr4["img_url"].ToString();
                    if (string.IsNullOrEmpty(picurl))
                    {
                        strTxt.Append(",\"img_url\":\"../image/error-img.png\"");
                    }
                    else if (picurl.Contains("shicaiquanzi"))
                    {
                        if (picurl.Contains("http://"))
                        {
                            strTxt.Append(",\"img_url\":\"" + picurl + "\"");

                        }
                        else { strTxt.Append(",\"img_url\":\"http://" + picurl + "\""); }
                        //strTxt.Append(",\"img_url\":\"http://" + picurl + "\"");
                    }
                    else
                    {
                        strTxt.Append(",\"img_url\":\"http://pic.shicaiquanzi.com" + picurl + "\"");
                    }
                    if (Convert.ToInt32(dr4["company_id"]) > 0)
                    {
                        strTxt.Append(",\"company\":\"" + new BLL.company().GetTitle(Convert.ToInt32(dr4["company_id"])).Replace("\\","") + "\"");
                    }
                    strTxt.Append("}");
                    //是否加逗号
                    if (i < ds4.Tables[0].Rows.Count - 1)
                    {
                        strTxt.Append(",");
                    }
                }
                strTxt.Append("]");
            }

            //获取信息
            BLL.xinxi xxbll = new BLL.xinxi();
            string strWhere = "id>0";
            if (type_id > 0)
            {
                strWhere += " and type_id=" + type_id;
            }
            ///添加产品分类筛选条件  Demon 2018-11-30
            if (category_id > 0)
            {
                strWhere += " and category_id=" + category_id;
            }
            DataSet xxds = xxbll.GetList(10, 1, strWhere, "add_time desc", out this.totalCount);
            int xx_total_page = this.totalCount / 10;
            strTxt.Append(",\"total_page\":" + xx_total_page.ToString());
            //如果记录存在
            if (xxds.Tables[0].Rows.Count > 0)
            {
                strTxt.Append(",\"xinxi_list\":[");
                for (int i = 0; i < xxds.Tables[0].Rows.Count; i++)
                {
                    if (i > 0 && i % 5 == 0)
                    {
                        Model.guanggao adModel = new BLL.guanggao().GetRandModelGlobal();
                        if (adModel != null)
                        {
                            strTxt.Append("{");
                            strTxt.Append("\"typeid\":2");
                            strTxt.Append(",\"id\":" + adModel.id);
                            strTxt.Append(",\"company_id\":\"" + adModel.company_id.ToString() + "\"");
                            strTxt.Append(",\"company_name\":\"" + new BLL.company().GetTitle(adModel.company_id) + "\"");
                            strTxt.Append(",\"adimg\":\"http://pic.shicaiquanzi.com" + adModel.img_url + "\"");
                            strTxt.Append("},");
                        }
                    }
                    DataRow dr3 = xxds.Tables[0].Rows[i];
                    strTxt.Append("{");
                    strTxt.Append("\"typeid\":1");
                    strTxt.Append(",\"type_id\":" + dr3["type_id"]);
                    strTxt.Append(",\"type_name\":\"" + new BLL.xinxi_type().GetTitle(Convert.ToInt32(dr3["type_id"]))+"\"");
                    strTxt.Append(",\"id\":" + dr3["id"]);
                    strTxt.Append(",\"remark\":\"" + Microsoft.JScript.GlobalObject.escape(dr3["content"].ToString()) + "\"");
                    strTxt.Append(",\"remark_con\":\"" + Microsoft.JScript.GlobalObject.encodeURI(dr3["content"].ToString()) + "\"");
                    strTxt.Append(",\"add_time\":\"" + Utils.DateStringFromNow(Convert.ToDateTime(dr3["add_time"])) + "\"");
                    strTxt.Append(",\"hits\":" + dr3["hits"]);
                    if (dr3["type_id"].ToString() == "3") { strTxt.Append(",\"size\":\"\""); }
                    else { strTxt.Append(",\"size\":\"" + dr3["size_long"] + "×" + dr3["size_wide"] + "×" + dr3["size_height"] + "mm\""); }
                    strTxt.Append(",\"user_id\":" + dr3["user_id"]);
                    if (Convert.ToInt32(dr3["user_id"]) > 0)
                    {
                        string _username = new BLL.users().GetUserName(Convert.ToInt32(dr3["user_id"]));
                        string _avatar = new BLL.users().GetAvatar(Convert.ToInt32(dr3["user_id"]));
                        string _group_id = new BLL.users().GetGroupID(Convert.ToInt32(dr3["user_id"])).ToString();
                        strTxt.Append(",\"user_name\":\"" + _username + "\"");
                        strTxt.Append(",\"group_id\":\"" + _group_id + "\"");
                        if (string.IsNullOrEmpty(_avatar))
                        {
                            strTxt.Append(",\"avatar\":\"../image/default_avatar.png\"");
                        }
                        else
                        {

                            if (_avatar.Contains("shicaiquanzi"))
                            {
                                if (_avatar.Contains("http://"))
                                {
                                    strTxt.Append(",\"avatar\":\"" + _avatar + "\"");
                                }
                                else { strTxt.Append(",\"avatar\":\"http://" + _avatar + "\""); }
                                //strTxt.Append(",\"avatar\":\"http://" + _avatar + "\"");
                            }
                            else
                            {
                                strTxt.Append(",\"avatar\":\"http://pic.shicaiquanzi.com" + _avatar + "\"");
                            }
                        }
                    }
                    BLL.xinxi_albums pbll = new BLL.xinxi_albums();
                    DataSet pds = pbll.GetLists(int.Parse(dr3["id"].ToString()));
                    if (pds.Tables[0].Rows.Count > 0)
                    {
                        int _count = pds.Tables[0].Rows.Count;
                        if (_count > 4)
                        {
                            _count = 4;
                        }
                        strTxt.Append(",\"pic_list\":[");
                        for (int ii = 0; ii < _count; ii++)
                        {
                            DataRow pdr = pds.Tables[0].Rows[ii];
                            string picurl = pdr["thumb_path"].ToString();
                            if (!picurl.Contains("http://"))
                            {
                                strTxt.Append("{\"picurl\":\"http://" + picurl + "\"}");
                            }
                            else
                            {
                                strTxt.Append("{\"picurl\":\"" + picurl + "\"}");
                            }
                            //是否加逗号
                            if (ii < _count - 1)
                            {
                                strTxt.Append(",");
                            }
                        }
                        strTxt.Append("]");
                    }
                    else
                    {
                        strTxt.Append(",\"pic_list\":[]");
                    }

                    strTxt.Append("}");
                    //是否加逗号
                    if (i < xxds.Tables[0].Rows.Count - 1)
                    {
                        strTxt.Append(",");
                    }
                }
                strTxt.Append("]");
            }
            else
            {
                strTxt.Append(",\"status\":2,\"xinxi_list\":[]");
            }

            strTxt.Append("}");
            context.Response.Write(strTxt.ToString());
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}